package serverce

import (
	"context"
	"fmt"
	"user_srv/basic/config"
	__ "user_srv/basic/proto"
	"user_srv/handler/models"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) AddVideo(_ context.Context, in *__.AddVideoReq) (*__.AddVideoResp, error) {
	var video models.Video
	video.UserId = uint(in.UserId)
	video.Title = in.Title
	video.Describe = in.Describe
	video.VideoUrl = in.VideoUrl
	video.VideoType = in.VideoType

	db := config.DB
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return nil, err
	}

	if err := video.AddVideo(tx); err != nil {
		tx.Rollback()
		return nil, err
	}
	var examine models.Examine
	examine.UserId = video.UserId
	examine.VideoId = video.Id
	if err := examine.AddExamine(tx); err != nil {
		tx.Rollback()
		return nil, err
	}

	if err := tx.Commit().Error; err != nil {
		return nil, err
	}

	return &__.AddVideoResp{
		VideoId: int64(video.Id),
	}, nil
}
func (s *Server) ListVideo(_ context.Context, in *__.ListVideoReq) (*__.ListVideoResp, error) {
	var video models.Video
	video.VideoType = in.VideoType
	var Lists []*__.ListVideo
	listVideo, _ := video.ListVideo(int(in.Page), int(in.PageSize))
	for _, v := range listVideo {
		Lists = append(Lists, &__.ListVideo{
			UserId:    int64(v.UserId),
			Title:     v.Title,
			Describe:  v.Describe,
			VideoUrl:  v.VideoUrl,
			VideoType: v.VideoType,
			VideoId:   int64(v.Id),
		})
	}
	return &__.ListVideoResp{List: Lists}, nil
}
func (s *Server) DetailVideo(_ context.Context, in *__.DetailVideoReq) (*__.DetailVideoResp, error) {
	var video models.Video
	video.Id = uint(in.VideoId)

	db := config.DB
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return nil, err
	}

	if err := video.DetailVideo(tx); err != nil {
		tx.Rollback()
		return nil, err
	}
	//
	if err := video.UpdatePlayCount(tx); err != nil {
		tx.Rollback()
		return nil, err
	}

	if err := tx.Commit().Error; err != nil {
		return nil, err
	}

	return &__.DetailVideoResp{
		UserId:    int64(video.UserId),
		Title:     video.Title,
		Describe:  video.Describe,
		VideoUrl:  video.VideoUrl,
		Status:    int64(video.Status),
		LikeCount: int64(video.LikeCount),
		PlayCount: int64(video.PlayCount),
	}, nil
}
func (s *Server) AddLike(_ context.Context, in *__.AddLikeReq) (*__.AddLikeResp, error) {
	var like models.Like
	like.UserId = uint(in.UserId)
	like.VideoId = uint(in.VideoId)

	err := like.GetLikeStatus()
	if err != nil {
		return nil, fmt.Errorf("获取信息失败")
	}
	if like.Id != 0 {
		return &__.AddLikeResp{
			Status: int64(like.Status),
		}, nil
	}

	db := config.DB
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err = tx.Error; err != nil {
		return nil, err
	}

	if err = like.AddLike(tx); err != nil {
		tx.Rollback()
		return nil, err
	}
	var video models.Video
	video.Id = uint(in.VideoId)
	if err = video.UpdatePLikeCount(tx); err != nil {
		tx.Rollback()
		return nil, err
	}

	if err = tx.Commit().Error; err != nil {
		return nil, err
	}

	return &__.AddLikeResp{
		Status: int64(like.Status),
	}, nil
}
func (s *Server) DeleteLike(_ context.Context, in *__.DeleteLikeReq) (*__.DeleteLikeResp, error) {
	var like models.Like
	like.Id = uint(in.LikeId)
	db := config.DB
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return nil, err
	}

	if err := like.DeleLike(tx); err != nil {
		tx.Rollback()
		return nil, err
	}
	var video models.Video
	video.Id = uint(in.LikeId)
	if err := video.DeleteLikeCount(tx); err != nil {
		tx.Rollback()
		return nil, err
	}

	if err := tx.Commit().Error; err != nil {
		return nil, err
	}

	return &__.DeleteLikeResp{
		LikeId: int64(like.Id),
	}, nil
}
func (s *Server) GetLikeCount(_ context.Context, in *__.GetLikeCountReq) (*__.GetLikeCountResp, error) {
	var video models.Video
	video.Id = uint(in.VideoId)
	err := video.GetLikeCount()
	if err != nil {
		return nil, fmt.Errorf("查询视频信息失败")
	}

	return &__.GetLikeCountResp{
		Title:     video.Title,
		Describe:  video.Describe,
		VideoUrl:  video.VideoUrl,
		LikeCount: int64(video.LikeCount),
	}, nil
}
